﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using UFIDA.U9.Base.FlexField.DescFlexField;
using UFIDA.U9.Base.UOM;
using UFIDA.U9.CBO.FI.CFPDTO;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.Pub.Controller.Proxy;
using UFIDA.U9.CBO.PubBE.YYC;
using UFIDA.U9.CBO.SCM.Bin;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.ProjectTask;
using UFIDA.U9.CBO.SCM.Supplier;
using UFIDA.U9.Cust.YYD.WebApi.Dtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.RequestDtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.ReturnDtos;
using UFIDA.U9.Cust.YYD.WebApi.Public;
using UFIDA.UBF.SystemManage;
using UFIDA.UBF.SystemManage.Agent;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC;
using UFSoft.UBF.PL.Engine.Cache;
using UFSoft.UBF.PL;
using UFSoft.UBF.Transactions;
using UFSoft.UBF.Util.DataAccess;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.PM.ASN;
using UFIDA.U9.CBO.DTOs;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.Base;
using UFIDA.U9.PM.ASN.Proxy;
using UFIDA.U9.PM.Enums;
using UFIDA.U9.PM.DTOs;
using UFIDA.U9.Lot;
using System.Security.Cryptography;
using UFIDA.U9.PM.Rcv.Proxy;
using UFIDA.U9.ISV.RCV.Proxy;
using UFIDA.U9.ISV.RCV.DTO;
using UFSoft.UBF.MVC.Common;
using static UFSoft.UBF.MVC.Helper.ZipHelper;

namespace UFIDA.U9.Cust.YYD.WebApi.Controllers
{
    public class WMSController : ApiController
    {
        [HttpPost]
        public ApiResult<List<ResultDTORData>> CreateRcvBySrcASN(RequestPara<List<CreateRcvByASNDTO>> paras)
        {
            using (BPForEngine bp1 = new BPForEngine())
            {
                PublicMethod.WriteContext(paras.Context, JsonConvert.SerializeObject(paras), this.GetType().Name);

                var createRcvByASNDTOs = paras.Data;
                //接口逻辑
                ApiResult<List<ResultDTORData>> rtn = new ApiResult<List<ResultDTORData>>();
                List<ResultDTORData> resultDTOs = new List<ResultDTORData>();
                ResultDTORData resultDTO = new ResultDTORData();
                foreach (CreateRcvByASNDTO createRcvBySrc in createRcvByASNDTOs)
                {
                    Receivement rcvItem = null;
                    List<UFIDA.U9.PM.ASN.ASNToRcvTransDTOData> asnToRcvDTODatas = ConvertToASNData(createRcvBySrc.aSNToRcvTransDTOs);
                    resultDTO = new ResultDTORData();
                    resultDTO.OtherID = createRcvBySrc.OtherID;
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        long vfID = 0L;
                        try
                        {
                            ASNToRcvBPProxy aSNToRcvBP = new ASNToRcvBPProxy();
                            aSNToRcvBP.ASNToRcvTransDTOs = asnToRcvDTODatas;
                            bool flag = createRcvBySrc.splitItems == null || (createRcvBySrc.splitItems.Count == 1 && string.IsNullOrEmpty(createRcvBySrc.splitItems[0]));
                            if (flag)
                            {
                                createRcvBySrc.splitItems = new List<string>();
                            }
                            aSNToRcvBP.SplitTerms = createRcvBySrc.splitItems;
                            //string jsonstr = JsonHelper.GetJsonJS(asnToRcvDTODatas);

                            RcvResultDTOData rcvResult = aSNToRcvBP.Do();
                            vfID = rcvResult.RcvKeys[0];
                            PLCacheManager.DataCache.FlushCache();
                            PLCacheManager.ObjectCache.FlushCache();
                            bool flag2 = vfID > 0L;
                            if (flag2)
                            {
                                rcvItem = Receivement.Finder.FindByID(vfID);
                                using (ISession session = Session.Open())
                                {
                                    bool flag3 = rcvItem != null;
                                    if (flag3)
                                    {
                                        rcvItem.DescFlexField.PrivateDescSeg1 = createRcvBySrc.WMSDocNo;
                                        rcvItem.ActivateType = ActivateTypeEnum.OBAUpdate;
                                        for (int i = 0; i < rcvItem.RcvLines.Count; i++)
                                        {
                                            bool flag4 = i == createRcvBySrc.aSNToRcvTransDTOs.Count;
                                            if (flag4)
                                            {
                                                break;
                                            }
                                            bool flag5 = rcvItem.RcvLines[i] != null && createRcvBySrc.aSNToRcvTransDTOs[i] != null;
                                            if (flag5)
                                            {
                                                bool flag6 = i == createRcvBySrc.aSNToRcvTransDTOs.Count;
                                                if (flag6)
                                                {
                                                    break;
                                                }
                                                bool flag7 = rcvItem.RcvLines[i] != null && createRcvBySrc.aSNToRcvTransDTOs[i] != null;
                                                if (flag7)
                                                {
                                                    int locationNo = 0;
                                                    bool flag8 = rcvItem.RcvLines[i].Wh != null && rcvItem.RcvLines[i].Wh.IsBin && createRcvBySrc.aSNToRcvTransDTOs[i].BinInfoList != null && createRcvBySrc.aSNToRcvTransDTOs[i].BinInfoList.Count > 0;
                                                    if (flag8)
                                                    {
                                                        foreach (UFIDA.U9.ISV.PM.RestSV.Model.SCM.PM.BinInfo binInfo in createRcvBySrc.aSNToRcvTransDTOs[i].BinInfoList)
                                                        {
                                                            Bin bin = null;
                                                            bool flag9 = binInfo.Bin != null && binInfo.Bin.ID > 0L;
                                                            if (flag9)
                                                            {
                                                                bin = Bin.Finder.FindByID(binInfo.Bin.ID);
                                                            }
                                                            else
                                                            {
                                                                bool flag10 = binInfo.Bin != null && !string.IsNullOrEmpty(binInfo.Bin.Code);
                                                                if (flag10)
                                                                {
                                                                    bin = Bin.Finder.Find("Org=@Org and Code=@Code", new OqlParam[]
                                                                    {
                                                                    new OqlParam("Org", Context.LoginOrg.ID),
                                                                    new OqlParam("Code", binInfo.Bin.Code)
                                                                    });
                                                                }
                                                            }
                                                            bool flag11 = bin != null && bin.ID > 0L;
                                                            if (flag11)
                                                            {
                                                                RcvLineLocation location = RcvLineLocation.Create(rcvItem.RcvLines[i]);
                                                                //location.SysState = 2;
                                                                locationNo = (location.LocationNo = locationNo + 10);
                                                                location.LocationKey = new Bin.EntityKey(bin.ID);
                                                                location.LocationQtyTU = binInfo.LocationQtyTU;
                                                                location.LocationQtySU = binInfo.LocationQtySU;
                                                            }
                                                        }
                                                    }
                                                    bool flag12 = createRcvBySrc.aSNToRcvTransDTOs[i].Project != null && (!string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].Project.Code) || createRcvBySrc.aSNToRcvTransDTOs[i].Project.ID > 0L);
                                                    if (flag12)
                                                    {
                                                        bool flag13 = createRcvBySrc.aSNToRcvTransDTOs[i].Project.ID > 0L;
                                                        if (flag13)
                                                        {
                                                            rcvItem.RcvLines[i].Project = Project.Finder.FindByID(createRcvBySrc.aSNToRcvTransDTOs[i].Project.ID);
                                                        }
                                                        else
                                                        {
                                                            rcvItem.RcvLines[i].Project = Project.Finder.Find("Code=@code and Org=" + Context.LoginOrg.ID.ToString(), new OqlParam[]
                                                            {
                                                            new OqlParam(createRcvBySrc.aSNToRcvTransDTOs[i].Project.Code)
                                                            });
                                                        }
                                                    }
                                                    bool flag14 = rcvItem.RcvLines[i].ItemInfo.ItemID.InventoryInfo != null && rcvItem.RcvLines[i].ItemInfo.ItemID.InventoryInfo.LotParamKey != null && rcvItem.RcvLines[i].Wh != null && rcvItem.RcvLines[i].Wh.IsLot && !string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].InvLotCode);
                                                    if (flag14)
                                                    {
                                                        rcvItem.RcvLines[i].InvLotCode = createRcvBySrc.aSNToRcvTransDTOs[i].InvLotCode;
                                                        LotMaster lot = LotMaster.Finder.Find("LotCode =@code and Item =@item", new OqlParam[]
                                                        {
                                                        new OqlParam("code", createRcvBySrc.aSNToRcvTransDTOs[i].InvLotCode),
                                                        new OqlParam("item", rcvItem.RcvLines[i].ItemInfo.ItemID.ID)
                                                        });
                                                        bool flag15 = lot == null;
                                                        if (flag15)
                                                        {
                                                            lot = LotMaster.Finder.Find("LotCode =@code and Item.Code =@itemcode", new OqlParam[]
                                                            {
                                                            new OqlParam("code", createRcvBySrc.aSNToRcvTransDTOs[i].InvLotCode),
                                                            new OqlParam("itemcode", rcvItem.RcvLines[i].ItemInfo.ItemCode)
                                                            });
                                                        }
                                                        bool flag16 = lot != null;
                                                        if (flag16)
                                                        {
                                                            rcvItem.RcvLines[i].InvLot = lot;
                                                        }
                                                        rcvItem.RcvLines[i].InvLotValidDate = createRcvBySrc.aSNToRcvTransDTOs[i].InvLotValidDate;
                                                        bool flag17 = !string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].InvLotEnableDate);
                                                        if (flag17)
                                                        {
                                                            rcvItem.RcvLines[i].InvLotEnableDate = DateTime.Parse(createRcvBySrc.aSNToRcvTransDTOs[i].InvLotEnableDate);
                                                        }
                                                        bool flag18 = !string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].InvDisabledTime);
                                                        if (flag18)
                                                        {
                                                            rcvItem.RcvLines[i].InvDisabledTime = DateTime.Parse(createRcvBySrc.aSNToRcvTransDTOs[i].InvDisabledTime);
                                                        }
                                                        else
                                                        {
                                                            bool flag19 = string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].InvDisabledTime) && !string.IsNullOrEmpty(createRcvBySrc.aSNToRcvTransDTOs[i].InvLotEnableDate) && createRcvBySrc.aSNToRcvTransDTOs[i].InvLotValidDate > 0;
                                                            if (flag19)
                                                            {
                                                                DateTime InvDisabledTime = DateTime.Parse(createRcvBySrc.aSNToRcvTransDTOs[i].InvLotEnableDate).AddDays((double)createRcvBySrc.aSNToRcvTransDTOs[i].InvLotValidDate);
                                                                rcvItem.RcvLines[i].InvDisabledTime = InvDisabledTime;
                                                            }
                                                        }
                                                    }
                                                    bool flag20 = rcvItem.RcvLines[i].RcvProcedure != null && createRcvBySrc.aSNToRcvTransDTOs[i].RcvProcedure > 0 && rcvItem.RcvLines[i].RcvProcedure.Value != createRcvBySrc.aSNToRcvTransDTOs[i].RcvProcedure;
                                                    if (flag20)
                                                    {
                                                        rcvItem.RcvLines[i].RcvProcedure = ReceiptModeEnum.GetFromValue(createRcvBySrc.aSNToRcvTransDTOs[i].RcvProcedure);
                                                    }
                                                }
                                            }
                                        }
                                        session.Commit();
                                    }

                                    //提交审核
                                    if (createRcvBySrc.DocStatus != 0)
                                    {

                                        //using (ISession session1 = Session.Open())
                                        //{
                                        //    rcvItem.Status = RcvStatusEnum.Approving;
                                        //    session1.Commit();
                                        //}


                                        RCVLogicKeyINFOData submmit = new RCVLogicKeyINFOData();
                                        submmit.RcvID = rcvItem.ID;
                                        submmit.DocNo = rcvItem.DocNo;
                                        submmit.Org = rcvItem.Org.ID;
                                        new ApproveRCVSRVProxy
                                        {
                                            ActType = 7,
                                            RCVLogicKeyINFOs = new List<RCVLogicKeyINFOData>()
                                            {
                                                submmit
                                            }
                                        }.Do();

                                        RCVLogicKeyINFOData rCVLogicKeyINFO = new RCVLogicKeyINFOData();
                                        rCVLogicKeyINFO.RcvID = rcvItem.ID;
                                        rCVLogicKeyINFO.DocNo = rcvItem.DocNo;
                                        rCVLogicKeyINFO.Org = rcvItem.Org.ID;
                                        new ApproveRCVSRVProxy
                                        {
                                            ActType = 8,
                                            RCVLogicKeyINFOs = new List<RCVLogicKeyINFOData>()
                                            {
                                                rCVLogicKeyINFO
                                            }
                                        }.Do();
                                    }
                                }
                                scope.Commit();
                                resultDTO.IsSucess = true;
                                resultDTO.ID = vfID;
                                bool flag21 = rcvItem != null;
                                if (flag21)
                                {
                                    resultDTO.Code = rcvItem.DocNo;
                                }
                                else
                                {
                                    resultDTO.Code = string.Empty;
                                }
                                resultDTOs.Add(resultDTO);
                            }
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTOs.Clear();
                            resultDTO.IsSucess = false;
                            resultDTO.Code = string.Empty;
                            resultDTO.ID = 0L;
                            resultDTO.ErrorMsg = ex.Message;
                            resultDTOs.Add(resultDTO);
                        }
                    }
                }
                rtn.Data = resultDTOs;

                return rtn;
            }

        }

        /// <summary>
        /// TransformDTO转换为TransformDTOData
        /// </summary>
        /// <param name="aSNToRcvTransDTOs">转换参数DTO</param>
        /// <returns></returns>
        // Token: 0x06000B25 RID: 2853 RVA: 0x0001F810 File Offset: 0x0001DA10
        public static List<UFIDA.U9.PM.ASN.ASNToRcvTransDTOData> ConvertToASNData(List<Dtos.ReturnDtos.ASNToRcvTransDTO> aSNToRcvTransDTOs)
        {
            List<UFIDA.U9.PM.ASN.ASNToRcvTransDTOData> dataList = new List<UFIDA.U9.PM.ASN.ASNToRcvTransDTOData>();
            foreach (Dtos.ReturnDtos.ASNToRcvTransDTO dto in aSNToRcvTransDTOs)
            {
                UFIDA.U9.PM.ASN.ASNToRcvTransDTOData dtoData = new UFIDA.U9.PM.ASN.ASNToRcvTransDTOData();
                dtoData.ASNLineKey = dto.ASNLineKey;
                dtoData.TransQty = new DoubleQuantityData();
                bool flag = dto.TransQty != null;
                if (flag)
                {
                    dtoData.TransQty.Amount1 = dto.TransQty.m_amount1;
                    dtoData.TransQty.Amount2 = dto.TransQty.m_amount2;
                    bool flag2 = dto.TransQty.m_uOM1 != null;
                    if (flag2)
                    {
                        dtoData.TransQty.UOM1 = new UOMInfoDTOData();
                        dtoData.TransQty.UOM1.Rate = dto.TransQty.m_uOM1.m_rate;
                        dtoData.TransQty.UOM1.UOMMain = dto.TransQty.m_uOM1.UOMMain;
                        dtoData.TransQty.UOM1.UOMMain_SKey = dto.TransQty.m_uOM1.UOMMain_SKey;
                        dtoData.TransQty.UOM1.UOMSub = dto.TransQty.m_uOM1.UOMSub;
                        dtoData.TransQty.UOM1.UOMSub_SKey = dto.TransQty.m_uOM1.UOMSub_SKey;
                    }
                    bool flag3 = dto.TransQty.m_uOM2 != null;
                    if (flag3)
                    {
                        dtoData.TransQty.UOM2 = new UOMInfoDTOData();
                        dtoData.TransQty.UOM2.Rate = dto.TransQty.m_uOM2.m_rate;
                        dtoData.TransQty.UOM2.UOMMain = dto.TransQty.m_uOM2.UOMMain;
                        dtoData.TransQty.UOM2.UOMMain_SKey = dto.TransQty.m_uOM2.UOMMain_SKey;
                        dtoData.TransQty.UOM2.UOMSub = dto.TransQty.m_uOM2.UOMSub;
                        dtoData.TransQty.UOM2.UOMSub_SKey = dto.TransQty.m_uOM2.UOMSub_SKey;
                    }
                }
                bool flag4 = dto.Wh != null;
                if (flag4)
                {
                    bool flag5 = dto.Wh.ID > 0L;
                    if (flag5)
                    {
                        Warehouse wh = Warehouse.Finder.FindByID(dto.Wh.ID);
                        bool flag6 = wh != null;
                        if (flag6)
                        {
                            dtoData.Warehouse = dto.Wh.ID;
                        }
                    }
                    else
                    {
                        bool flag7 = !string.IsNullOrEmpty(dto.Wh.Code);
                        if (flag7)
                        {
                            Warehouse wh = Warehouse.FindByCode(Context.LoginOrg, dto.Wh.Code);
                            bool flag8 = wh != null;
                            if (flag8)
                            {
                                dtoData.Warehouse = wh.ID;
                            }
                        }
                    }
                }
                dataList.Add(dtoData);
            }
            return dataList;
        }

    }




}
